package com.bingxue.edu.management.shop.model.resp;

import com.bingxue.edu.common.enums.CouponValidTypeEnum;
import com.bingxue.edu.management.shop.model.entity.Coupon;
import com.bingxue.edu.management.shop.model.entity.CouponTemplate;
import com.mybatisflex.annotation.RelationOneToOne;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * @author panbo
 * @since 2024/8/27
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "优惠券详情")
public class CouponDetail extends Coupon {
    
    @RelationOneToOne(selfField = "couponTemplateId", targetField = "id")
    private CouponTemplate template;

    public LocalDateTime getStartValidTime() {
        if (template.getValidType() == CouponValidTypeEnum.FIXED) {
            return template.getStartValidTime();
        } else {
            return this.getReceiveTime();
        }
    }

    public LocalDateTime getEndValidTime() {
        if (template.getValidType() == CouponValidTypeEnum.FIXED) {
            return template.getEndValidTime();
        } else {
            return this.getReceiveTime().plusDays(template.getValidDays());
        }
    }
}
